from sqlalchemy import Column, Integer, String, Text, Date, DateTime, ForeignKey, UniqueConstraint
from sqlalchemy.orm import relationship

from src.models.base import TimeModel


class RespositoryStatus(TimeModel):
    __tablename__ = 'respository_status'

    id = Column(Integer, primary_key=True)
    repo_id = Column(Integer, ForeignKey('repository.id'), comment='代码仓')
    watch_change_num = Column(Integer, comment='watch_change数量')
    star_change_num = Column(Integer, comment='star_change数量')
    fork_change_num = Column(Integer, comment='fork_change数量')
    scan_date = Column(Date, comment='日期')

    repo = relationship('Repository', back_populates='repository_statuses')

    __table_args__ = (
        UniqueConstraint('repo_id', 'scan_date', name='uq_repo_scan_date'),
    )

    def __str__(self):
        return f'{self.repo.name}-{self.scan_date}'


class Issue(TimeModel):
    __tablename__ = 'issue'

    id = Column(Integer, primary_key=True)
    unique_tag = Column(Integer, unique=True, comment='issue唯一标识')
    repo_id = Column(Integer, ForeignKey('repository.id'), comment='代码仓')
    title = Column(String(256), comment='标题')
    desc = Column(Text, comment='描述')
    author_id = Column(Integer, ForeignKey('user.id'), comment='作者')
    status = Column(Integer, comment='状态')  # todo枚举值跟随gitcode
    priority = Column(Integer, comment='优先级')  # todo枚举值跟随gitcode
    delay_duration = Column(Integer, comment='停留时长（s）')
    first_resp_duration = Column(Integer, comment='首次响应时长（s）')
    delayed_reply_duration = Column(Integer, comment='超期回复时长（s）')
    first_resp_time = Column(DateTime, nullable=True, comment='首次响应时间')
    last_resp_time = Column(DateTime, nullable=True, comment='最新响应时间')
    close_loop_time = Column(DateTime, nullable=True, comment='闭环时间')
    updated = Column(DateTime, comment='gitcode更新时间')
    created = Column(DateTime, comment='gitcode创建时间')

    repo = relationship('Repository', back_populates='issues')
    author = relationship('User', back_populates='issues')

    def __str__(self):
        return f'{self.repo.name}-{self.updated}'


class PullRequest(TimeModel):
    __tablename__ = 'pull_request'

    id = Column(Integer, primary_key=True)
    unique_tag = Column(Integer, unique=True, comment='pr唯一标识')
    repo_id = Column(Integer, ForeignKey('repository.id'), comment='代码仓')
    title = Column(String(256), comment='标题')
    desc = Column(Text, comment='描述')
    author_id = Column(Integer, ForeignKey('user.id'), comment='作者')
    branch = Column(String(256), comment='分支')
    updated = Column(DateTime, comment='gitcode更新时间')
    created = Column(DateTime, comment='gitcode创建时间')

    repo = relationship('Repository', back_populates='pull_requests')
    author = relationship('User', back_populates='pull_requests')
    advices = relationship('Advice', back_populates='pull_request')

    def __str__(self):
        return f'{self.repo.name}-{self.updated}'


class Advice(TimeModel):
    __tablename__ = 'advice'

    id = Column(Integer, primary_key=True)
    unique_tag = Column(Integer, unique=True, comment='advice唯一标识')
    pull_request_id = Column(Integer, ForeignKey('pull_request.id'), comment='所属Pr')
    author_id = Column(Integer, ForeignKey('user.id'), comment='作者')
    comment = Column(Text, comment='内容')
    updated = Column(DateTime, comment='gitcode更新时间')
    created = Column(DateTime, comment='gitcode创建时间')

    pull_request = relationship('PullRequest', back_populates='advices')
    author = relationship('User', back_populates='advices')

    def __str__(self):
        return f'{self.author.username}-{self.updated}'